//
//  Data.m
//  作业
//
//  Created by 刘崇 on 16/10/13.
//  Copyright © 2016年 刘崇. All rights reserved.
//

#import "Data.h"
#import "ClassModel.h"
#import "GDataXMLNode.h"

static Data *data;
@implementation Data
{
    NSMutableArray *_arr;
    ClassModel * _class;
    NSString *_bqname;
}
//创建单例
+(instancetype)sharedData
{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        data = [[Data alloc]init];
    });
    return data;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (!data)
    {
        data = [super allocWithZone:zone];
    }
    return data;
}
-(id)copy
{
    return self;
}
-(id)mutableCopy
{
    return self;
}
//
-(void)shujuchuli:(NSString *)url andBJ:(NSString *)bj
{
    NSURL * myurl = [NSURL URLWithString:url];
    NSURLRequest * requst = [[NSURLRequest alloc]initWithURL:myurl];
    if ([bj isEqualToString:@"SAX"])
    {
        //
        [NSURLConnection sendAsynchronousRequest:requst queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            //SAX解析
            NSXMLParser * xml = [[NSXMLParser alloc]initWithData:data];
            //开始解析
            xml.delegate = self;
            [xml parse];
        }];
    }
    else
    {
        [NSURLConnection sendAsynchronousRequest:requst queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
            GDataXMLElement *root = [doc rootElement];
            //存放信息
            ClassModel *model = [[ClassModel alloc]init];
            //遍历
            for (GDataXMLElement *classele in [root elementsForName:@"class"]) {
                ClassModel *class = [[ClassModel alloc]init];
                class.chengcai = [[[classele elementsForName:@"chengcai"] firstObject] stringValue];
                class.weiji = [[[classele elementsForName:@"weiji"] firstObject] stringValue];
                class.kouhao = [[[classele elementsForName:@"kouhao"] firstObject] stringValue];
                class.idname = [[[classele elementsForName:@"id"] firstObject] stringValue];
                class.name = [[[classele elementsForName:@"image"] firstObject] stringValue];
                if ([class.idname isEqualToString:bj])
                {
                    model = class;
                }
            }
            //解析完毕 回到主线程发送通知
            dispatch_async(dispatch_get_main_queue(), ^{
                //将数据传递给城市页面
                [[NSNotificationCenter defaultCenter] postNotificationName:@"Banjixinxi" object:model];
            });
            
        }];

    }
}
#pragma mark -- delegate
//开始解析
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    _arr = [[NSMutableArray alloc]init];
}
//结束解析
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        //将数据传递给页面
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Banji" object:_arr];
    });
}
//遇到开始标签
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"class"])
    {
        _class = [[ClassModel alloc]init];
        [_arr addObject:_class];
    }
    //记录标签名字
    _bqname = elementName;
}
//遇到结束标签
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName
{
    //将标记制空
    _bqname = nil;
}
//遇到内容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([_bqname isEqualToString:@"name"])
    {
        _class.name = string;
    }
    else if ([_bqname isEqualToString:@"id"])
    {
        _class.idname = string;
    }
}
@end
